/** 时间轴插件 */
(function($){
    $.fn.timeline = function(options){
        var $this = $(this);
 
        $(this).addClass('timeline')
            . append('<div class="time-line">');
 
        $('ul > li > div', this).each(function(index){
            $(this).after('<div style="clear: both;">')
                . wrap('<div class="wrap"></div>')
                . append(
                    '<div class="arrow"></div>' +
                    '<div class="circle"></div>')
                . addClass('node');
 
            if (index % 2 == 0)
                $(this).addClass('left')
                    . parent().parent().addClass('left');
            else
                $(this).addClass('right')
                    . parent().parent().addClass('right');
        });
 
        $(window).resize(function(){
            $('.time-line').height($('.timeline').height());
        });
    };
})(jQuery);
$(document).ready(function(){
    $('.time-line').height($('.timeline').height());
});
$('#timeline').timeline();

String.prototype.trim= function(){  
    return this.replace(/(^\s*)|(\s*$)/g, '');  
}

$('.crud .add').click(function(){
    $('#add-name').focus();
});
$('.crud .delete').each(function(){
    $(this).click(function(){
        $('#delete-id').val($(this).attr('data-id'));
        $('#update-name').focus();
    });
});
$('.crud .edit').each(function(){
    $(this).click(function(){
        var id = $(this).attr('data-id');
        $('#update-id').val(id);
        $('#update-name').val($('#item-' + id).text().trim());
    });
});

(function(){
    // 从url读取json 
    function getJSON(type) {
        var json;
        $.ajax({
            dataType: "json",
            async: false,
            url: 'index.php',
            data: { m: 'Api', a: type },
            success: function(data) {
                json = data;
            }
        });
        return json;
    }
    // 添加select的选项
    function addOpt(data, $sel, id) {
        $sel.empty();
        for (i in data) {
            var $option = $('<option></option>');
            if (id == data[i].id)
                $option.prop('selected', true);
            $option.val(data[i].id).text(data[i].name)
                . appendTo($sel);
        }
    }
    // 读取json到变量
    var session = getJSON('session');
    var department = getJSON('department');
    var post = getJSON('post');

    // jquery变量
    var $edit_session = $('#edit-session');
    var $edit_department = $('#edit-department');
    var $edit_post = $('#edit-post');

    // 获取初始id
    var session_id = $edit_session.attr('data-id');
    var department_id = $edit_department.attr('data-id');
    var post_id = $edit_post.attr('data-id');
    
    // 初始化select
    addOpt(session, $edit_session, session_id);
    addOpt(department[session_id], $edit_department, department_id);
    addOpt(post[department_id], $edit_post, post_id);

    // 绑定change事件
    $edit_session.change(function(){
        var id = $(this).val();
        if (department[id] == undefined) {
            addOpt([{id: -1, name: '没有可选部门'}], $edit_department, 0);
            $edit_department.prop('disabled', true).change();
        }
        else {
            addOpt(department[id], $edit_department, 0);
            $edit_department.prop('disabled', false).change();
        }
    });
    $edit_department.change(function(){
        var id = $(this).val();
        if (post[id] == undefined) {
            addOpt([{id: -1, name: '没有可选部门'}], $edit_post, 0);
            $edit_post.prop('disabled', true);
        }
        else {
            addOpt(post[id], $edit_post, 0);
            $edit_post.prop('disabled', false);
        }
    });

})();
